using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualBasic.FileIO;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace Windows_Explorer.Forms
{
    class clsSearch
    {
        public static int i = 0, j = 0;
        public static void searchDirectory(DirectoryInfo dir, string name)
        {
            if (dir.GetDirectories() != null)
            {
                if (dir.GetFiles() != null)
                {
                    foreach (FileInfo fi in dir.GetFiles())
                    {
                        if (fi.Name.ToUpper().Contains(name.ToUpper().Trim()))
                        {
                            long size = fi.Length / 1024;
                            string[] s = { fi.Name, fi.Extension.ToUpper(), size + " KB", fi.LastWriteTime.ToString(), fi.FullName };
                            ListViewItem item = new ListViewItem(s);
                            item.ImageKey = "File";
                            Program.fmain.lvListView.Items.Add(item);
                        }
                    }
                }
                foreach (DirectoryInfo di in dir.GetDirectories())
                {
                    if (di.Name.ToUpper().Contains(name.ToUpper().Trim()))
                    {
                        string[] subFolder = { di.Name, "File Folder", "", di.LastWriteTime.ToString(), di.FullName, di.Parent.FullName };
                        ListViewItem item = new ListViewItem(subFolder);
                        item.ImageKey = "Folder";
                        Program.fmain.lvListView.Items.Add(item);
                    }
                    searchDirectory(di, name);
                }
                
            }
        }
        public static void SearchFile(DirectoryInfo dir, string fileName, string fileType)
        {
            if (dir.GetDirectories() != null)
            {
                foreach (DirectoryInfo di in dir.GetDirectories())
                {
                    SearchFile(di, fileName, fileType);
                }
                if (dir.GetFiles() != null)
                {
                    foreach (FileInfo fi in dir.GetFiles())
                    {
                        if (fi.Name.ToUpper().Contains(fileName.ToUpper().Trim()) && fileType.ToString().ToUpper().Contains(fi.Extension.ToUpper()))
                        {
                            long size = fi.Length / 1024;
                            string[] s = { fi.Name, fi.Extension.ToUpper(), size + " KB", fi.LastWriteTime.ToString(), fi.FullName };
                            ListViewItem item = new ListViewItem(s);
                            item.ImageKey = "File";
                            Program.fmain.lvListView.Items.Add(item);
                        }
                    }
                }
            }
        }

        public static void SearchFile(DirectoryInfo dir, string fileName, DateTime dateTime1,string fileType)
        {
            if (dir.GetDirectories() != null)
            {
                foreach (DirectoryInfo di in dir.GetDirectories())
                {
                    SearchFile(di, fileName, dateTime1, fileType);
                }
                if (dir.GetFiles() != null)
                {
                    foreach (FileInfo fi in dir.GetFiles())
                    {
                        if (fi.Name.ToUpper().Contains(fileName.ToUpper().Trim()) && fi.LastWriteTime > dateTime1 && fileType.ToString().ToUpper().Contains(fi.Extension.ToUpper()))
                        {
                            long size = fi.Length / 1024;
                            string[] s = { fi.Name, fi.Extension.ToUpper(), size + " KB", fi.LastWriteTime.ToString(), fi.FullName };
                            ListViewItem item = new ListViewItem(s);
                            item.ImageKey = "File";
                            Program.fmain.lvListView.Items.Add(item);
                        }
                    }
                }
            }
        }
        
        //Ham search co tim kiem theo * (vd fileName = string1 + '*' + string2)
        //Co the khong co ky tu *
        public static void SearchFilePro(DirectoryInfo dir, string fileName)
        {
            if (dir.GetDirectories() != null)
            {
                string[] fileArr = fileName.Split('*');
                if (dir.GetFiles() != null)
                {
                    foreach (FileInfo fi in dir.GetFiles())
                    {
                        int k = 0;
                        for (int i = 0; i < fileArr.Length; i++)
                        {
                            if (fi.Name.ToUpper().Contains(fileArr[i].ToUpper().Trim()))
                            {
                                k++;
                            }
                        }
                        if (k == fileArr.Length)
                        {
                            string name = fi.Name;
                            int[] index = new int[fileArr.Length - 1];
                            for (int j = 0; j < fileArr.Length - 1; j++)
                            {
                                index[j] = name.ToUpper().IndexOf(fileArr[j].ToUpper());
                            }
                            bool b = true;
                            for (int j = 0; j < fileArr.Length - 2; j++)
                            {
                                for (int l = j + 1; l < fileArr.Length - 1; l++)
                                {
                                    if (index[j] > index[l]) b = false;
                                }
                            }
                            if (b)
                            {
                                long size = fi.Length / 1024;
                                string[] s = { fi.Name, fi.Extension.ToUpper(), size + " KB", fi.LastWriteTime.ToString(), fi.FullName };
                                ListViewItem item = new ListViewItem(s);
                                item.ImageKey = Class.clsTreeView.ImageKey(fi);
                                Program.fmain.lvListView.Items.Add(item);
                            }
                        }
                    }
                }
               
                foreach (DirectoryInfo di in dir.GetDirectories())
                {
                    int k = 0;
                    for (int i = 0; i < fileArr.Length; i++)
                    {
                        if (di.Name.ToUpper().Contains(fileArr[i].ToUpper().Trim()))
                        {
                            k++;
                        }
                    }
                    if (k == fileArr.Length)
                    {
                        string name = di.Name;
                        int[] index = new int[fileArr.Length - 1];
                        for (int j = 0; j < fileArr.Length - 1; j++)
                        {
                            index[j] = name.ToUpper().IndexOf(fileArr[j].ToUpper());
                        }
                        bool b = true;
                        for (int j = 0; j < fileArr.Length - 2; j++)
                        {
                            for (int l = j + 1; l < fileArr.Length - 1; l++)
                            {
                                if (index[j] > index[l]) b = false;
                            }
                        }
                        if (b)
                        {
                            string[] subFolder = { di.Name, "File Folder", "", di.LastWriteTime.ToString(), di.FullName, di.Parent.FullName };
                            ListViewItem item = new ListViewItem(subFolder);
                            item.ImageKey = "Folder";
                            Program.fmain.lvListView.Items.Add(item);
                        }
                    }
                    SearchFilePro(di, fileName);
                }
            }
        }
    }
}
